package com.parenting.service.impl;

import com.parenting.entities.CouponUsageRecordDO;
import com.parenting.feign.controller.IFeignAppletOrderController;
import com.parenting.feign.entities.OrderDTO;
import com.parenting.mapper.CouponUsageRecordMapper;
import com.parenting.request.wechat.WechatRecordRequest;
import com.parenting.response.wechat.CouponUsageRecord;
import com.parenting.response.wechat.WechatDateRecordResponse;
import com.parenting.response.wechat.WechatRecordResponse;
import com.parenting.service.CouponUsageRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠券使用记录表 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-27
 */
@Service
public class CouponUsageRecordServiceImpl extends ServiceImpl<CouponUsageRecordMapper, CouponUsageRecordDO> implements CouponUsageRecordService {

    @Override
    public List<WechatDateRecordResponse> consumptionRecordList(WechatRecordRequest wechatRecordRequest, String shopId) {
        List<WechatDateRecordResponse> wechatDateRecordResponses = baseMapper.dateRecordResponse(wechatRecordRequest, shopId);
        if (null!=wechatDateRecordResponses&&wechatDateRecordResponses.size()>0){
            for (WechatDateRecordResponse r:wechatDateRecordResponses) {
                List<WechatRecordResponse> wechatRecordResponses = baseMapper.recordResponse(wechatRecordRequest, shopId, r.getPaymentDate());
                r.setRecord(wechatRecordResponses);
            }
        }
        return wechatDateRecordResponses;
    }
    @Autowired
    private IFeignAppletOrderController feignAppletOrderController;
    @Override
    public CouponUsageRecord getCouponUsageRecord(String usageRecordId) {

        CouponUsageRecordDO couponUsageRecordDO = baseMapper.selectById(usageRecordId);
        if (null!=couponUsageRecordDO){
            CouponUsageRecord couponUsageRecord=new CouponUsageRecord();
            couponUsageRecord.setCouponCode(couponUsageRecordDO.getCouponCode());
            couponUsageRecord.setCouponName(couponUsageRecordDO.getCouponName());
            OrderDTO orderById = feignAppletOrderController.getOrderById(couponUsageRecordDO.getOrderId());
            if (null!= orderById){
                couponUsageRecord.setPayer(orderById.getBuyerName());
                couponUsageRecord.setActivityName(orderById.getActivityName());
                couponUsageRecord.setPaymentTime(orderById.getPaymentTime());
                couponUsageRecord.setOriginalPrice(orderById.getOriginalPrice());
                couponUsageRecord.setItemConcessional(orderById.getItemConcessional()*orderById.getItemNumber());
                couponUsageRecord.setPaid(orderById.getPaid());
            }
            return couponUsageRecord;
        }
        return null;
    }


}
